package com.example.b_308.software.model.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.b_308.software.OfficeResult;
import com.example.b_308.software.comment.view.title.util.JsonUtils;
import com.example.b_308.software.view.base.INetCallBack;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;


import org.apache.http.entity.StringEntity;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by B-308 on 2017/11/16.
 */
public class BaseModel implements Serializable {

    HttpUtils http = new HttpUtils();
    String URL = "http://10.5.10.201:8082";
    Map<String, Object> attrs=new HashMap<>();

    public BaseModel() {
    }

    public BaseModel(String json) {
        attrs = JsonUtils.jsonToT(json, Map.class);
    }

    public <T extends BaseModel> void getRequestHttp(String url, T t, final INetCallBack iNetCallBack) {
        http.send(HttpRequest.HttpMethod.GET, URL + paraseURL(url, t), new RequestCallBack<Object>() {
            public void onSuccess(ResponseInfo<Object> responseInfo) {
                String json = responseInfo.result.toString();
                OfficeResult officeResult = new OfficeResult(json);
                iNetCallBack.onNetCallBack(officeResult);
            }

            public void onFailure(HttpException e, String s) {
            }
        });

    }

    public Map<String, Object> getAttrs() {
        return attrs;
    }

    protected <T extends BaseModel> String paraseURL(String url, T t) {
        StringBuilder sb = new StringBuilder();
        sb.append(url);
        Map<String, Object> map = t.getAttrs();
        if (null != map && !map.isEmpty()) {
            sb.append("?");
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                sb.append(key + "=" + value + "&");
            }
            return sb.substring(0, sb.lastIndexOf("&"));
        }
        return sb.toString();
    }

    public <T extends BaseModel> void postRequestHttp(String url, T t, final INetCallBack iNetCallBack) {
        http.send(HttpRequest.HttpMethod.GET, URL + paraseURL(url, t), new RequestCallBack<Object>() {
            public void onSuccess(ResponseInfo<Object> responseInfo) {
                iNetCallBack.onNetCallBack(new OfficeResult(responseInfo.result.toString()));
            }

            public void onFailure(HttpException e, String s) {
            }
        });

    }


    public <T extends BaseModel> void postData(String path, T t, final INetCallBack in) {
        http.send(HttpRequest.HttpMethod.POST, URL + path, paraseParams(t), new RequestCallBack<Object>() {
            @Override
            public void onSuccess(ResponseInfo<Object> responseInfo) {
                String json = responseInfo.result.toString();
                OfficeResult officeResult = new OfficeResult(json);
                in.onNetCallBack(officeResult);
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    private <T extends BaseModel> RequestParams paraseParams(T t) {
        RequestParams requestParams = new RequestParams();
        requestParams.addHeader("Content_Type", "application/json");
        if (null == t || null == t.getAttrs() || 0 == t.getAttrs().size()) {
            return requestParams;
        }
        JSONObject jsonObject = new JSONObject(t.getAttrs());
        try {
            requestParams.setBodyEntity(new StringEntity(jsonObject.toJSONString()));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return requestParams;
    }

    public <T extends BaseModel> void getData(String url, T t, final INetCallBack in) {
        http.send(HttpRequest.HttpMethod.GET, getUrl(url, t), new RequestCallBack<Object>() {
            @Override
            public void onSuccess(ResponseInfo<Object> responseInfo) {
                String json = responseInfo.result.toString();
                OfficeResult officeResult = new OfficeResult(json);
                in.onNetCallBack(officeResult);
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    private <T extends BaseModel> String getUrl(String url, T t) {
        StringBuilder sb = new StringBuilder();
        sb.append(url);
        if (null != t) {
            Map<String, Object> map = t.getAttrs();
            if (null != map && map.size() > 0) {
                sb.append("?");
                for (Map.Entry<String, Object> entity : map.entrySet()) {
                    String key = entity.getKey();
                    Object value = entity.getValue();
                    sb.append(key + "=" + value + "&");
                }
                return sb.substring(0, sb.lastIndexOf("&"));
            }
        }
        return sb.toString();
    }

    public <H> H get(String key) {
        return (H) attrs.get(key);
    }

    public <T> void put(String key, T t) {
        attrs.put(key, t);
    }

    public void setAttrs(Map<String, Object> attrs) {
        put("attrs", attrs);
        this.attrs = attrs;
    }

    public <T extends BaseModel> void putAttrs(T t) {
        if (null != t)
            attrs.putAll(t.getAttrs());
    }
}
